package com.langchain4j.controller;

import com.langchain4j.config.AIConfig;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiImageModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

@RestController
public class Test {

    @Resource
    private OpenAiChatModel chatModel;
    @Resource
    private OpenAiImageModel openAiImageModel;
    @Resource
    private OpenAiStreamingChatModel openAiStreamingChatModel;
    @Resource
    private AIConfig.IATest iaTest;

    @GetMapping("/test")
    public Flux<String> chat(@RequestParam String message, HttpServletResponse response) throws ExecutionException, InterruptedException, TimeoutException {
        /*TokenStream tokenStream = iaTest.chat(1,message);
        CompletableFuture<ChatResponse> futureResponse = new CompletableFuture<>();
        tokenStream.onPartialResponse(System.out::print)
                .onCompleteResponse(futureResponse::complete)
                .onError(futureResponse::completeExceptionally)
                .start();*/
        response.setCharacterEncoding("utf-8");
        return iaTest.chat2(1,message);

        // 自定义结构化提示词
        /*String legal = "婚姻法";
        AIConfig.LegalPrompt legalPrompt = new AIConfig.LegalPrompt();
        legalPrompt.setLegal(legal);
        legalPrompt.setQuestion(message);
        return iaTest.chat3(1,legalPrompt,legal);*/
    }

    @GetMapping("/test1")
    public String chat1(@RequestParam String message, HttpServletResponse response) throws ExecutionException, InterruptedException, TimeoutException {
        response.setCharacterEncoding("utf-8");
        return iaTest.chat1(2,message);
    }

    /*@Value("classpath:static/images/1.png")
    private org.springframework.core.io.Resource resource;
    @GetMapping("/image/call")
    public String readImageContent() throws ExecutionException, InterruptedException, TimeoutException, IOException {
        byte[] byteArray = resource.getContentAsByteArray();
        String base64Data = Base64.getEncoder().encodeToString(byteArray);
        *//*UserMessage userMessage = UserMessage.builder()
            .addText("从以下图片中获取来源网站名称和9.30号上证指数")
            .addImageUrls()
            .build();*//*

        dev.langchain4j.data.message.UserMessage userMessage = dev.langchain4j.data.message.UserMessage.from(
                TextContent.from("What do you see?"),
                ImageContent.from("https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png")
        );

        Response<AiMessage> response = chatModel.chat(userMessage);
        System.out.println(response.content().text());
        return response.content().text();
    }*/
}
